import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpRequest } from "../../../service/httpRequest";
import { HideElementService } from "../../../service/hideElement.service";
import { PageModel } from "../../../model/page-model";
import { flyIn } from '../../../animations/fly-in';
import { GlobalVariable } from '../../../global.config';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { FileLoader } from "../../../service/fileLoader";
declare var $:any;
@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.css'],
  animations: [
    flyIn
  ]
})
export class AccountComponent implements OnInit {
  elc = {a:'新增用户',e:'编辑用户',d:'删除用户',r:'分配角色',p:'分配产品'};
  tool = GlobalVariable.tooltip;
  data = [];
  companyData = [];
  keyword:any;
  pageModel = new PageModel();
  page:number = 1;

  @ViewChild('myModal') public myModal:ModalDirective;
  buttonType:number;
  disabled:string = "";


  model = {id:'',company_id:'',username:'',password:'',nick_name:'',email:'',mobile:'',remark:'',facing_img:''};


  constructor(private httpRequest:HttpRequest,private fileLoader:FileLoader,private he:HideElementService) {
    this.elc = this.he.hideElement(this.elc);
  }

  ngOnInit() {
    this.getData();
    this.getCompanyData();
    this.fileLoader.LoadFileList(['assets/js/uploadify/jquery.uploadify.min.js'],()=>{})
  }

  getData(){
    this.httpRequest.sendGet('account/page',{page:this.page}).subscribe(res=>{
      this.data = res.result.aaData;      
      this.pageModel.setPage({'iTotalRecords':res.result.iTotalRecords});
    },err=>console.log(err));
  }

  getCompanyData(){
    this.httpRequest.sendGet('companys/list').subscribe(res=>{
      this.companyData = res.result;      
    },err=>console.log(err));
  }


  newModal(){
    this.initUploader();
    this.buttonType=1;
    this.model = {id:'',company_id:'',username:'',password:'',nick_name:'',email:'',mobile:'',remark:'',facing_img:''};
    $("#file_uploaded").text('');
    this.myModal.show();
  }

  confirmNew():void{

    this.disabled='disabled';
    this.httpRequest.sendPost('account',this.model,'op').subscribe(res=>{
      alert('用户创建成功');
      this.getData();
      this.myModal.hide();
      this.disabled='';
    },err=>{
      alert('用户创建失败');
      this.disabled='';
    });
  }

  editModal(item){
    this.initUploader();
    this.buttonType=2;
    this.model = item;
    $("#file_uploaded").text('');
    this.myModal.show();
  }
  confirmEdit():void{
    this.disabled='disabled';
    this.httpRequest.sendPost('account/'+this.model.id,this.model,'op').subscribe(res=>{
      alert('用户修改成功');
      this.getData();
      this.myModal.hide();

      this.disabled='';
    },err=>{
      alert('用户修改失败');
      this.disabled='';
    });
  }

  deleteModal(item){
    this.buttonType=3;
    this.model = item;
    this.myModal.show();
  }

  confirmDelete(){
    //this.httpRequest.sendDelete('products/'+this.model.id).subscribe(res=>{
    //  alert("产品删除成功！");
    //  this.myModal.hide();
    //  this.disabled='';
    //  this.getData();
    //},err => {alert('产品删除失败!');this.disabled='';console.log(err)});
    this.myModal.hide();
  }


  //初始化图片上传控件
  initUploader() {
    let that =this;
    $('#file_upload').uploadify({
      'swf'      : 'assets/js/uploadify/uploadify.swf',
      'uploader' : "http://101.37.24.73:8080/fileupload/",
      'buttonText': '上传账户图片',
      'fileObjName' : 'file',
      'fileTypeDesc' : '选择文件',
      'fileTypeExts' : '*.jpg;*.png;',
      'method' : 'post',
      'fileSizeLimit':'10MB',
      'auto' : true,
      'multi' : false,
      'onUploadSuccess' : (file,data,response) => {
        let filedata = JSON.parse(data);
        that.model.facing_img = filedata.file_path;
        $("#file_uploaded").text(filedata.file_path+"上传完成");
      }
    });

  }

}
